<template>
  <div>
    <van-empty v-if="addressList.length == 0" description="暂无地址" />
    <van-address-list
      v-model="chosenAddressId"
      :list="addressList"
      default-tag-text="默认"
      @add="onAdd"
      @edit="onEdit"
      @select="select"
    >
    </van-address-list>
  </div>
</template>

<script>
import { reqAddresslist } from "../../http/http";
export default {
  mounted() {
    this.chosenAddressId = this.$route.query.id*1;
    this.getAddressList();
  },
  data() {
    return {
      chosenAddressId: "",
      addressList: [],
    };
  },
  methods: {
    async getAddressList() {
      let { uid } = JSON.parse(localStorage.getItem("isLogin"));
      let { data: res } = await reqAddresslist({ uid });
      if (res.code == 200) {
        let arr = res.list ? res.list : [];
        console.log(arr);
        this.addressList = arr.map((item) => {
          return {
            id: item.id,
            name: item.username,
            tel: item.userphone,
            address: item.location + item.useraddress,
            isDefault: item.status == 1,
          };
        });
      }
    },
    onAdd() {
      this.$router.push("/addressAdd");
    },
    onEdit(item, index) {
      this.$router.push({path:"/addressAdd",query:{id:item.id}})
    },
    select(item){
      this.$router.push("/order?addressId=" + item.id);
    }
  },
};
</script>

<style>
</style>